﻿using System.ComponentModel;

namespace System.Reflection
{
    /// <summary>
    /// 兼容高版本
    /// </summary>
    public static class PropertyInfoExtensions
    {
#if NET40
        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetValue(this PropertyInfo propertyInfo, object obj)
        {
            return propertyInfo.GetValue(obj, null);
        }
#endif
        /// <summary>
        /// 获取属性的 <see cref="DescriptionAttribute"/> 的说明文本
        /// </summary>
        /// <param name="property"></param>
        /// <returns>当 <see cref="DescriptionAttribute.Description"/> 为 Null、Empty、WhiteSpace 时均返回 <see langword="null"/></returns>
        public static string GetDescription(this PropertyInfo property)
        {
            var description = property.GetCustomAttribute<DescriptionAttribute>()?.Description;
            if (string.IsNullOrWhiteSpace(description))
            { return null; }
            return description;
        }
    }
}
